package andy.demo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import android.R;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Andy_AndroidDemoActivity extends ListActivity {
    /** Called when the activity is first created. */
	
	public static final String EXTRAS_ANDY_DEMO = "andy.demo";
	public static final String CATEGORY = "andy.demo.category";
	public static final String TITLE= "title";
	private static final String INTENT = "intent";
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);  
        Intent intent = getIntent();
        String path = intent.getStringExtra(EXTRAS_ANDY_DEMO);
        
        if(path == null) {
        	path = "";
        }
        
        setListAdapter(new SimpleAdapter(Andy_AndroidDemoActivity.this, getData(path),android.R.layout.simple_list_item_1, new String[]{TITLE}, new int[] { android.R.id.text1 }));
        
        
    }

	private List getData(String prefix) {
		
		List<HashMap> myData = new ArrayList<HashMap>();
		
		//解析包
		
		Intent queryIntent = new Intent();
		queryIntent.setAction(Intent.ACTION_MAIN);
		queryIntent.addCategory(CATEGORY);
		
		
		//
		String [] prefixPath;
		if(prefix != null && !"".equalsIgnoreCase(prefix)) {
			prefixPath = prefix.split("/");
		}else {
			prefixPath = null;
		}
		List<ResolveInfo> list = getPackageManager().queryIntentActivities(queryIntent, 0);
		if(list == null) return myData;
		
		 Map<String, Boolean> entries = new HashMap<String, Boolean>();
		
		for (ResolveInfo resolveInfo : list) {
			
//			String label = (String) resolveInfo.loadLabel(getPackageManager());
			//标签 
			CharSequence labelSeq = resolveInfo.loadLabel(getPackageManager());
			
			 String label = labelSeq != null
	                    ? labelSeq.toString()
	                    : resolveInfo.activityInfo.name;
	                    
			
			if(prefix == "" || label.startsWith(prefix)){
				
				String[] labelPath = label.split("/");
				
				String nextLabel = (prefixPath == null ? labelPath[0] : labelPath[prefixPath.length]);
				
				if((prefixPath != null ? prefixPath.length : 0) == labelPath.length-1) {
					
					addItem(myData, nextLabel, activityIntent(
                            resolveInfo.activityInfo.applicationInfo.packageName,
                            resolveInfo.activityInfo.name));
				
				}else {
					if(entries.get(nextLabel) == null) {
						addItem(myData, nextLabel, brouseIntent("".equalsIgnoreCase(prefix) ?  nextLabel : prefix + "/" +nextLabel));
					}
					
					entries.put(nextLabel, true);
				}
				
				
			}
		}
		
		
		
		return myData;
	}

	private Intent brouseIntent(String prefix) {
		Intent intent = new Intent();
		intent.setClass(this, Andy_AndroidDemoActivity.class);
		intent.putExtra(EXTRAS_ANDY_DEMO, prefix);
		
		return intent;
	}

	private void addItem(List<HashMap> myData, String nextLabel,
			Intent activityIntent) {
		HashMap<String,Object> map = new HashMap<String, Object>();
		map.put(TITLE, nextLabel);
		map.put(INTENT, activityIntent);
		myData.add(map);
		}

	private Intent activityIntent(String packageName, String componentName) {
		
		Intent intent = new Intent();
		intent.setClassName(packageName, componentName);
		
		return intent;
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		
//		Map map = (Map) l.getItemAtPosition(position);
		Map map = (Map) l.getAdapter().getItem(position);

        Intent intent = (Intent) map.get(INTENT);
        startActivity(intent);
		
	}
}